package com.example.netty.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * Socket套接字 客户端1
 *
 * @author LSQ
 * @since 2023/2/16 21:09
 */
public class Client1 {

    public static void main(String[] arg) {
        BufferedReader br = null;
        PrintWriter pw = null;
        try {
            Socket socket = new Socket("localhost", 100);
            //获取输入流与输出流
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            pw = new PrintWriter(socket.getOutputStream(), true);

            while (true) {
                //向服务器发送数据
                Scanner scan=new Scanner(System.in);
                System.out.println("请输入");
                pw.println(scan.nextLine());
                //接收服务器发回的消息
                String str = br.readLine();
                System.out.println(str);

                if ("bye".equals(str)) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
